class Utils {
    verification(msg, arr) {
        for (let item of arr) {
            let key = item[0];
            let type = item[1];
            let value = msg[key];
            if (typeof value !== type) { //类型验证 string ,number, Boolean, undefined, null, object
                return key;
            }
            if (type === 'string') {
                if (value.length === 0) {
                    return key;
                } else if (item[2]) {
                    let re = item[2];
                    if (!re.test(value)) { //正则验证
                        return key;
                    }
                }
            } else if (type === 'number') {
                if (value < 0 || !Number.isInteger(value)) { //不能小于0,必须是整形
                    return key;
                }
            }
        }
        return
    }

    //从数组里面获得随机一组不重复的数字
    getRandomArrayNoRepeat(num, array) {
        let arrayCopy = array.slice(0)
        let length = arrayCopy.length
        var k = []
        for (var i = 0; i < num; i++) {
            var l = Math.floor(Math.random() * length);
            length = length - 1
            k.push(arrayCopy[l])
            arrayCopy[l] = arrayCopy[length]
        }
        return k
    }

    //从数组里面获得随机一组不重复的数字,删除原数组获取到的数字
    getRandomArrayAndRemove(num, array) {
        let length = array.length
        var k = []
        for (var i = 0; i < num; i++) {
            var l = Math.floor(Math.random() * length);
            length = length - 1
            k.push(array[l])
            array[l] = array[length];
            array.pop();
        }
        return k
    }

    //从数组里面获得随机一组数字,可以重复
    getRandomArrayRepeat(num, array) {
        let length = array.length;
        var k = [];
        for (var i = 0; i < num; i++) {
            var l = Math.floor(Math.random() * length);
            k.push(array[l]);
        }
        return k;
    }

    getOneFromArray(array) {
        return array[Math.floor(Math.random() * array.length)];
    }


    getOneFromArrayAndRemove(array) {
        let index = Math.floor(Math.random() * array.length);
        let value = array[index];
        array.splice(index, 1);
        return value;
    }

    getRandomNumber(min, max) {
        max = Math.floor(max)
        min = Math.floor(min)
        let n = max + min
        let x1 = Math.floor(Math.random() * (max - min + 1) + min)
        let x2 = Math.floor(Math.random() * (max - min + 1) + min)
        let x3 = x1 + x2
        let m = x3 > n ? x3 : 2 * n - x3
        return m - max
    }

    getRandom(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min);
    }

    addRandomHalf2(value) {
        let max = Math.ceil(value + Math.random() * value / 5)
        let min = Math.ceil(value)
        if (max === min) {
            max += 1
        }
        let n = max + min
        let x1 = Math.floor(Math.random() * (max - min + 1) + min)
        let x2 = Math.floor(Math.random() * (max - min + 1) + min)
        let x3 = x1 + x2
        let m = x3 > n ? x3 : 2 * n - x3
        return m - max
    }

    itemArrayCopy(items) {
        let temp = [];
        for (let i in items) {
            temp.push(items[i].slice(0))
        }
        return temp;
    }
}

let utils = new Utils();

export default utils;